const config  = require('../../config/index')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseUrl: config.baseUrl, 
    idCardPicFront: null,
    idCardPicBack: null,
    formData: {
      idCard: '',
      userName: '',
      validityPeriod: ''
    },
    theme: wx.getStorageSync('InvoiceLoanTheme') || ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      theme: wx.getStorageSync('InvoiceLoanTheme') || ''
    })
  }, 
  onShow(){}, 
  handleCamera(e){
    const { type } = e.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/idCardCamera/index?type=${type}`,
    })
    // wx.chooseMedia({
    //   count: 1,
    //   mediaType: ['image'],
    //   sourceType: ['camera'],
    //   success(res){
    //     const token = wx.getStorageSync('InvoiceLoanToken')
    //     wx.uploadFile({
    //       filePath: res.tempFiles[0].tempFilePath,
    //       name: 'file',
    //       header:{
    //         Authorization: token,
    //         'Content-Type': 'multipart/form-data',
    //       },
    //       url: `${config.baseUrl}/ocr/id/card`,
    //       formData:{
    //         field: type
    //       },
    //       success(uploadRes){
    //         const data = JSON.parse(uploadRes.data)
    //         if(data.code === 200) {
    //           let aa = data.data[type==='idCardPicFront'?'idCardNo':'effPeriod']
    //           const formData = {
    //             ...that.data.formData,
    //           }
    //           if(type!=='idCardPicFront'){
    //             let startDate = ''
    //             let endDate = ''
    //             const arr = aa.split('-')
    //             startDate = arr[0].replace(/(\d{4})(\d{2})(\d{2})/, "$1-$2-$3")
    //             endDate = arr[1].replace(/(\d{4})(\d{2})(\d{2})/, "$1-$2-$3")
    //             aa = `${startDate}-${endDate}`
    //           } else {
    //             formData.userName = data.data.userName
    //           }
    //           that.setData({
    //             formData:{
    //               ...formData,
    //               [type==='idCardPicFront'?'idCard':'validityPeriod']: aa,
    //             }
    //           })
    //         } else {
    //           wx.showToast({
    //             title: data.message,
    //             icon:'none'
    //           })
    //         }
    //       }
    //     })
    //     that.setData({
    //       [type]:res.tempFiles[0],
    //     })
    //   }
    // })
  },
  // 简易双向数据绑定
  handInputChange (e) {
    this.setData({
      [`formData.${e.target.id}`]: e.detail.value
    })
  },
  handleNext() {
    const { formData,idCardPicFront,idCardPicBack } = this.data
    const customerInfoStr = wx.getStorageSync('InvoiceLoanCustomerInfo')
    const customerInfo = JSON.parse(customerInfoStr || '{}')
    if(!customerInfo.idCardNo || customerInfo.idCardNo !== formData.idCard) {
      wx.setStorage({
        key: 'InvoiceLoanFormData',
        data: JSON.stringify({
          formData: {},
          businessLicensePath: '',
          creditPath: '',
          liveAddr: ''
        })
      })
    }
    if(!idCardPicFront) {
      wx.showToast({
        icon:'none',
        title: '请上传身份证人像面',
      })
      return
    }
    if(!idCardPicBack) {
      wx.showToast({
        icon:'none',
        title: '请上传身份证国徽面',
      })
      return
    }
    if(!formData.idCard) {
      wx.showToast({
        icon:'none',
        title: '请输入身份证号',
      })
      return
    }
    if(!formData.validityPeriod) {
      wx.showToast({
        icon:'none',
        title: '请输入有效期',
      })
      return
    }
    const reg = /(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}$)/
    const isPass = reg.test(formData.idCard)
    if(!isPass) {
      wx.showToast({
        icon:'none',
        title: '请输入正确的身份证号码',
      })
      return
    }
    wx.setStorageSync('InvoiceLoanCustomerInfo', JSON.stringify({
      idCardNo: formData.idCard,
      userName: formData.userName
    }))
    wx.navigateTo({
      url: '/pages/realAuthentication/index',
    })
  }
})